package com.py.credit.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.AuthorizonLog;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.AuthorizationLogTableFieldEnum;
import com.py.credit.service.AuthorizationLogService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping(value = "/authorizationSearchLog")
public class AuthorizationSearchLogController {

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Autowired
	private AuthorizationLogService authorizationLogService;

	private final static String FRONT_TABLE_AUTH_LOG_ID = "autlogTable";

	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {

		return new RestServiceResult<List<OptionalColumn>>(AuthorizationLogTableFieldEnum.getOptionalColumns());

	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(
				AuthorizationLogTableFieldEnum.getAdvancedConditionColumns());

	}

	@RequestMapping(value = "/getLogDetail.json")
	public RestServiceResult<AuthorizonLog> getLogDetail(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {

		AuthorizonLog authLog = authorizationLogService.getBy(id);

		return new RestServiceResult<AuthorizonLog>(authLog);

	}

	@RequestMapping(value = "/getTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String[] configColumns = null;
		SysUser userInfo = user.getData();
		
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			
			if (null != userInfo) {

				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_AUTH_LOG_ID);
			} else {

				ExceptionLogger.error("AuthorizationSearchLogController#getTableList", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("AuthorizationSearchLogController#getTableList", "未获取到当前用户信息！");
		}

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? AuthorizationLogTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);

		List<Map<String, Object>> tableList = authorizationLogService.getByConditionForPage(showColumns,
				new String[] { "id" }, condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

	}

	@RequestMapping(value = "/addShowColunms.json")
	public RestServiceResult<String> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_AUTH_LOG_ID, columns);
			} else {

				ExceptionLogger.error("ContentController#addShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ContentController#addShowColunms", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/export.json")
	public void export(@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletResponse response) throws IOException {

		page.setLimit(1000);

		List<String> showColumns = JSONArray.parseArray(columnEN, String.class);
		List<Map<String, Object>> tableList = authorizationLogService.getByConditionForPage(showColumns, null,
				condition, page);

		List<OptionalColumn> optionalColumns = AuthorizationLogTableFieldEnum.getOptionalColumns();

		ExcelUtil.downloadExcel(response, tableList, "授权查询日志导出列表", "授权查询日志导出列表", showColumns, optionalColumns);

	}

}
